package com.hzw.saas.service.project.flow.service;

import java.util.Objects;
import javax.annotation.Resource;

import com.hzw.saas.common.flow.exception.FlowServiceHandleException;
import com.hzw.saas.common.flow.pojo.model.FlowInstance;
import com.hzw.saas.common.flow.pojo.model.FlowProcessRecord;
import com.hzw.saas.common.flow.service.FlowActionHandle;
import com.hzw.saas.common.flow.service.IFlowInstanceService;
import com.hzw.saas.service.project.model.ProjectApply;
import com.hzw.saas.service.project.service.IProjectApplyService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import static com.hzw.saas.service.project.constants.ProjectConsts.PROJECT_APPLY_SERVICE_REMOVE;

/**
 * 用户点击删除项目申请按钮
 * @author sonam
 * @sine 2021/8/11 6:41 下午
 */
@Service(PROJECT_APPLY_SERVICE_REMOVE)
public class ProjectApplyServiceRemoveImpl implements FlowActionHandle {

    @Resource
    private IFlowInstanceService flowInstanceService;

    @Resource
    private IProjectApplyService projectApplyService;

    @Override
    public void handle(FlowInstance flowInstance,FlowProcessRecord flowProcessRecord) throws FlowServiceHandleException {
        if(Objects.isNull(flowProcessRecord)) {
            throw new FlowServiceHandleException("业务参数丢失");
        }
        String flowInstanceId = flowProcessRecord.getFlowInstanceId();
        if(StrUtil.isBlank(flowInstanceId)) {
            throw new FlowServiceHandleException("业务参数y异常");
        }
        // 删除项目申请
        flowInstanceService.lambdaUpdate().eq(FlowInstance::getPid, flowProcessRecord.getFlowInstanceId()).remove();
        projectApplyService.lambdaUpdate().eq(ProjectApply::getFlowInstanceId, flowProcessRecord.getFlowInstanceId()).remove();
    }

}
